//============================================================================
//
// saHostManager.js
// ----------------
//
// This file is part of the API for the s-audit internals. This module
// performs host management utiltites, accessed by the "host" subcommand
// from the CLI. Each function returns a simple object, which is presented
// by a callback in the client layer for presentation.
//
//============================================================================

// We need a database connection and a config file.

var sqlite3 = require('sqlite3').verbose();
var config = require('jsonconfig');

config.load(['config/config.json']);

// Get a database connection and make sure foreign keys are enforced. The
// path to the database file is in the config file.

var db = new sqlite3.Database(config.DB, function() {
    db.run('PRAGMA foreign_keys=on');
});

//----------------------------------------------------------------------------
//

module.exports = {

	hostList: function(callback) {
	
		// Gets a list of all hosts in the database, paired with the group
		// to which they belong

		db.all('SELECT hosts.hostname AS host, audit_groups.name AS \
		groupname FROM hosts, audit_groups WHERE hosts.audit_group = \
		audit_groups.id ORDER BY hosts.hostname', callback);

	},

	hostCheckpoints: function(group, host, callback) {

		// Get checkpoints for the given host. Returns an object where each
		// item is the timestamp of the checkpoint.

		db.get('SELECT hosts.id FROM hosts WHERE hosts.hostname = ? AND \
		hosts.audit_group = (SELECT id FROM audit_groups WHERE name = ?)',
		host, group, function(err, row) {

			if (row === undefined) {
				callback(null, undefined);
			}
			else {
				db.all('SELECT timestamp FROM checkpoints WHERE host_id = ?',
				row.id, callback);
			}

		});

	},

	hostDelete: function(group, host, callback) {
		
		// Delete a host from the database. Cascading foreign keys in the
		// databse will remove the zones which belong to the host, and all
		// the audit data belonging to those zones.

		db.run('DELETE FROM hosts WHERE hosts.id = (SELECT hosts.id FROM \
		hosts, audit_groups WHERE hosts.audit_group = audit_groups.id AND \
		hosts.hostname = ? AND audit_groups.name = ?)', host, group, callback);
	},

	hostMove: function(group, host, target, callback) {
		
		// Move a host from one group to another. Assume that any detailed
		// error checking is done by the calling function.

		db.run('UPDATE hosts SET audit_group = (SELECT id FROM \
		audit_groups WHERE name = ?) WHERE hostname = ? AND audit_group \
		= (SELECT id FROM audit_groups WHERE name =?)', target, host, group,
		callback);
	},

	hostZones: function(group, host, callback) {

		// Get an object which lists the zones belonging to the specified
		// host

		db.all('SELECT zones.zone_name FROM zones, hosts, audit_groups \
		WHERE audit_groups.name = ? AND audit_groups.id=hosts.audit_group \
		AND hosts.hostname = ? AND hosts.id = zones.parent ORDER BY \
		zones.zone_name', group, host,
		callback);
	}

}
